[ty] remove special handling for Any() in match class patterns#23011
Merged
sharkdp merged 1 commit intoastral-sh:mainfrom Feb 2, 2026
Merged
[ty] remove special handling for Any() in match class patterns#23011sharkdp merged 1 commit intoastral-sh:mainfrom
Any() in match class patterns#23011sharkdp merged 1 commit intoastral-sh:mainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
Any() in match class patterns
1942284 to
23ffd7f
Compare
sharkdp
reviewed
Feb 2, 2026
Comment on lines
-121
to
-146
| ## Class patterns where the class pattern does not resolve to a class | ||
|
|
||
| In general this does not allow for narrowing, but we make an exception for `Any`. This is to support | ||
| [real ecosystem code](https://github.com/jax-ml/jax/blob/d2ce04b6c3d03ae18b145965b8b8b92e09e8009c/jax/_src/pallas/mosaic_gpu/lowering.py#L3372-L3387) | ||
| found in `jax`. | ||
|
|
||
| ```py | ||
| from typing import Any | ||
|
|
||
| X = Any | ||
|
|
||
| def f(obj: object): | ||
| match obj: | ||
| case int(): | ||
| reveal_type(obj) # revealed: int | ||
| case X(): | ||
| reveal_type(obj) # revealed: Any & ~int | ||
|
|
||
| def g(obj: object, Y: Any): | ||
| match obj: | ||
| case int(): | ||
| reveal_type(obj) # revealed: int | ||
| case Y(): | ||
| reveal_type(obj) # revealed: Any & ~int | ||
| ``` | ||
|
|
Contributor
There was a problem hiding this comment.
Link to relevant discussion here: https://github.com/astral-sh/ruff/pull/21150/files#r2749631869
sharkdp
approved these changes
Feb 2, 2026
Contributor
sharkdp
left a comment
There was a problem hiding this comment.
This looks good to me, thank you!
I'll keep the referenced ticket open for now, because I think it makes sense to introduce a new diagnostic.
carljm
added a commit
that referenced
this pull request
Feb 2, 2026
* main: (48 commits) add info for non_octal permissions (#22972) Fix empty body rule rendering (#23039) [ty] Infer `ParamSpec` from class constructors for callable protocols (#22853) Update NPM Development dependencies (#23030) Update CodSpeedHQ/action action to v4.8.2 (#23029) [ty] remove special handling for `Any()` in match class patterns (#23011) Update Rust crate get-size2 to v0.7.4 (#23022) Update Rust crate insta to v1.46.1 (#23023) Update taiki-e/install-action action to v2.67.11 (#23033) Update Rust crate colored to v3.1.1 (#23031) Update cargo-bins/cargo-binstall action to v1.17.3 (#23028) Update Rust crate uuid to v1.20.0 (#23032) [ty] Avoid using `.node()` for detecting `Self` (#23000) Update Rust crate proc-macro2 to v1.0.106 (#23024) Update actions/setup-python action to v6.2.0 (#23027) [ty] fix query cycles in decorated function with parameter defaults (#23014) Update Rust crate quote to v1.0.44 (#23025) Update Rust crate thiserror to v2.0.18 (#23026) Update Rust crate filetime to v0.2.27 (#23021) Update Rust crate clearscreen to v4.0.3 (#23020) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Anydoes not supportisinstancechecks and cannot be used in class patterns. If the arm withAny()is hit, it will raise an error at runtime:Because this fails at runtime, we should ideally raise a diagnostic on seeing
Any()in a match pattern. That could potentially be done as part of astral-sh/ty#2592. But for now, this PR only removes the special support that we have.The special-case handling does not reduce any diagnostics on
jaxas of today either.Test Plan
Removed tests :)